package com.github.caijh.mall.utils;

import java.util.Map;

import com.github.caijh.commons.util.Maps;
import com.github.caijh.mall.constants.Constants;
import com.github.caijh.mall.exception.CrmebException;

public class WxPayUtil {

    private WxPayUtil() {

    }

    /**
     * 处理 HTTPS API返回数据，转换成Map对象。return_code为SUCCESS时，验证签名。
     *
     * @param xmlStr API返回的XML格式数据
     * @return Map类型数据
     */
    public static Map<String, Object> processResponseXml(String xmlStr) {
        Map<String, Object> respData = Maps.fromXml(xmlStr);

        Object returnCode = respData.get("return_code");
        if (returnCode == null) {
            throw new CrmebException(String.format("No `return_code` in XML: %s", xmlStr));
        }
        if (!(Constants.FAIL.equals(returnCode) || Constants.SUCCESS.equals(returnCode))) {
            throw new CrmebException(String.format("returnCode value %s is invalid in XML: %s", returnCode, xmlStr));
        }

        return respData;
    }

}
